home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / egrep.zip / COMPAT-S.C next >
C/C++ Source or Header  |  1988-03-31  |  3KB  |  110 lines

  1. /*
  2.  * regcomp(), regexec(), and xread() were posted to mod.sources by
  3.  * henry spencer, and are too large to be included here.
  4.  *
  5.  * This file contains strcspn, strchr, strpbrk, and getopt.
  6.  */
  7.  
  8. #include <stdio.h>
  9. /*
  10.  * strcspn - find length of initial segment of s1 consisting entirely
  11.  * of characters not from s2
  12.  */
  13.  
  14. int strcspn(s1, s2)
  15. char *s1;
  16. char *s2;
  17. {
  18.     register char *scan1;
  19.     register char *scan2;
  20.     register int count;
  21.  
  22.     count = 0;
  23.     for (scan1 = s1; *scan1 != '\0'; scan1++) {
  24.         for (scan2 = s2; *scan2 != '\0';)    /* ++ moved down. */
  25.             if (*scan1 == *scan2++)
  26.                 return(count);
  27.         count++;
  28.     }
  29.     return(count);
  30. }
  31.  
  32. char *strchr(a, b)
  33. char *a, b;
  34.     {
  35.     char *index();
  36.     return (index(a, b));
  37.     }
  38.  
  39. /* strpbrk - Returns a pointer to the first character of source that is any
  40.  *  of the specified keys, or NULL if none of the keys are present
  41.  *  in the source string.
  42.  */
  43. char *strpbrk(source, keys)
  44. char *source, *keys;
  45. {
  46.         register int loc = 0, key_index = 0;
  47.  
  48.         while (source[loc] != '\0') {
  49.           key_index = 0;
  50.           while (keys[key_index] != '\0')
  51.             if (keys[key_index++] == source[loc])
  52.               return((char *) (source + loc));
  53.           loc++;
  54.         }
  55.         
  56.         return(NULL);
  57. }
  58.  
  59.  
  60. /*
  61.  * getopt - get option letter from argument vector
  62.  */
  63. int    opterr = 1,        /* useless, never set or used */
  64.     optind = 1,        /* index into parent argv vector */
  65.     optopt;            /* character checked for validity */
  66. char    *optarg;        /* argument associated with option */
  67.  
  68. #define BADCH    (int)'?'
  69. #define EMSG    ""
  70. #define tell(s)    fputs(*nargv,stderr);fputs(s,stderr); \
  71.         fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);
  72.  
  73. getopt(nargc,nargv,ostr)
  74. int    nargc;
  75. char    **nargv,
  76.     *ostr;
  77. {
  78.     static char    *place = EMSG;    /* option letter processing */
  79.     register char    *oli;        /* option letter list index */
  80.     char    *index();
  81.  
  82.     if(!*place) {            /* update scanning pointer */
  83.         if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) return(EOF);
  84.         if (*place == '-') {    /* found "--" */
  85.             ++optind;
  86.             return(EOF);
  87.         }
  88.     }                /* option letter okay? */
  89.     if ((optopt = (int)*place++) == (int)':' || !(oli = index(ostr,optopt))) {
  90.         if(!*place) ++optind;
  91.         tell(": illegal option -- ");
  92.     }
  93.     if (*++oli != ':') {        /* don't need argument */
  94.         optarg = NULL;
  95.         if (!*place) ++optind;
  96.     }
  97.     else {                /* need an argument */
  98.         if (*place) optarg = place;    /* no white space */
  99.         else if (nargc <= ++optind) {    /* no arg */
  100.             place = EMSG;
  101.             tell(": option requires an argument -- ");
  102.         }
  103.          else optarg = nargv[optind];    /* white space */
  104.         place = EMSG;
  105.         ++optind;
  106.     }
  107.     return(optopt);            /* dump back option letter */
  108. }
  109.  
  110.